home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / DLISPSHORT.PY < prev    next >
Encoding:
Text File  |  1999-07-28  |  5.1 KB  |  161 lines

  1. # Grammar generation 
  2. # for lisp lists with strings, ints, vars, print, and setq
  3.  
  4. # set this variable to regenerate the grammar on each load
  5. REGENERATEONLOAD = 1
  6.  
  7. import string
  8. GRAMMARSTRING ="""
  9.        Value ::  ## indicates Value is the root nonterminal for the grammar
  10.          @R SetqRule :: Value >> ( setq var Value )
  11.          @R ListRule :: Value >> ( ListTail
  12.          @R TailFull :: ListTail >> Value ListTail
  13.          @R TailEmpty :: ListTail >> )
  14.          @R Varrule :: Value >> var
  15.          @R Intrule :: Value >> int
  16.          @R Strrule :: Value >> str
  17.          @R PrintRule :: Value >> ( print Value )
  18. """
  19. COMPILEDFILENAME = "TESTLispG.py"
  20. MARSHALLEDFILENAME = "TESTLispG.mar"
  21. LISPCOMMENTREGEX = ";.*"
  22. INTREGEX = "["+string.digits+"]+"
  23. STRREGEX = '"[^\n"]*"'
  24. VARREGEX = "["+string.letters+"]["+string.letters+string.digits+"]*"
  25.  
  26. ### declare interpretation functions and regex's for terminals
  27. def intInterp( str ):
  28.     return string.atoi(str)
  29. def stripQuotes( str ):
  30.     return str[1:len(str)-1]
  31. def echo(string):
  32.     return string
  33. def DeclareTerminals(Grammar):
  34.     Grammar.Addterm("int", INTREGEX, intInterp)
  35.     Grammar.Addterm("str", STRREGEX, stripQuotes)
  36.     Grammar.Addterm("var", VARREGEX, echo)
  37.  
  38. ### declare the rule reduction interpretation functions.
  39. def EchoValue( list, Context ):
  40.     return list[0]
  41. def VarValue( list, Context ):
  42.     varName = list[0]
  43.     if Context.has_key(varName):
  44.        return Context[varName]
  45.     else:
  46.        raise NameError, "no such lisp variable in context "+varName
  47. def NilTail( list, Context ):
  48.     return []
  49. def AddToList( list, Context ):
  50.     return [ list[0] ] + list[1]
  51. def MakeList( list, Context ):
  52.     return list[1]
  53. def DoSetq( list, Context):
  54.     Context[ list[2] ] = list[3]
  55.     return list[3]
  56. def DoPrint( list, Context ):
  57.     print list[2]
  58.     return list[2]
  59. def BindRules(Grammar):
  60.     Grammar.Bind( "Intrule", EchoValue )
  61.     Grammar.Bind( "Strrule", EchoValue )
  62.     Grammar.Bind( "Varrule", VarValue )
  63.     Grammar.Bind( "TailEmpty", NilTail )
  64.     Grammar.Bind( "TailFull", AddToList )
  65.     Grammar.Bind( "ListRule", MakeList )
  66.     Grammar.Bind( "SetqRule", DoSetq )
  67.     Grammar.Bind( "PrintRule", DoPrint )
  68.  
  69. # This function generates the grammar and dumps it to a file.  
  70. def GrammarBuild():
  71.     import kjParseBuild
  72.     LispG = kjParseBuild.NullCGrammar()
  73.     LispG.SetCaseSensitivity(0) # grammar is not case sensitive for keywords
  74.     DeclareTerminals(LispG)
  75.     LispG.Keywords("setq print")
  76.     LispG.punct("().")
  77.     LispG.Nonterms("Value ListTail")
  78.     LispG.comments([LISPCOMMENTREGEX])
  79.     LispG.Declarerules(GRAMMARSTRING)
  80.     LispG.Compile()
  81.  
  82.     print "dumping as python to "+COMPILEDFILENAME
  83.     outfile = open(COMPILEDFILENAME, "w")
  84.     LispG.Reconstruct("LispG",outfile,"GRAMMAR")
  85.     outfile.close()
  86.  
  87.     print "dumping as binary to "+MARSHALLEDFILENAME
  88.     outfile = open(MARSHALLEDFILENAME, "w")
  89.     LispG.MarshalDump(outfile)
  90.     outfile.close()
  91.  
  92.     BindRules(LispG)
  93.     return LispG
  94.  
  95. # this function initializes the compiled grammar from the generated file.  
  96. def LoadLispG():
  97.     import TESTLispG
  98.     # reload to make sure we get the most recent version!
  99.     # (only needed when debugging the grammar).
  100.     reload(TESTLispG)
  101.     LispG = TESTLispG.GRAMMAR()
  102.     DeclareTerminals(LispG)
  103.     BindRules(LispG)
  104.     return LispG
  105.  
  106. def unMarshalLispG():
  107.     import kjParser
  108.     infile = open(MARSHALLEDFILENAME, "r")
  109.     LispG = kjParser.UnMarshalGram(infile)
  110.     infile.close()
  111.     DeclareTerminals(LispG)
  112.     BindRules(LispG)
  113.     return LispG
  114.  
  115. ########## test the grammar generation
  116. if REGENERATEONLOAD:
  117.    print "(re)generating the LispG grammar in file TESTLispG.py"
  118.    Dummy = GrammarBuild()
  119.    print "(re)generation done."
  120.  
  121. print "loading grammar as python"
  122. LispG = LoadLispG()
  123. ### declare an initial context, and do some tests.
  124. Context = { 'x':3 }
  125. test1 = LispG.DoParse1( '()', Context)
  126. test2 = LispG.DoParse1( '(123)', Context)
  127. test3 = LispG.DoParse1( '(x)', Context)
  128. test4 = LispG.DoParse1( '" a string "', Context)
  129. test5 = LispG.DoParse1( '(setq y (1 2 3) )', Context )
  130. test6 = LispG.DoParse1( '(SeTq x ("a string" "another" 0))', Context )
  131. test7str = """
  132.   ; this is a lisp comment
  133.   (setq abc (("a" x)
  134.              ("b" (setq d 12))
  135.              ("c" y) ) ; another lisp comment
  136.   )
  137. """
  138. test7 = LispG.DoParse1( test7str, Context)
  139. test8 = LispG.DoParse1( '(print (1 x d))', Context)
  140.  
  141. print "unmarshalling the grammar"
  142. LispG2 = unMarshalLispG()
  143. ### declare an initial context, and do some tests.
  144. Context = { 'x':3 }
  145. test1 = LispG2.DoParse1( '()', Context)
  146. test2 = LispG2.DoParse1( '(123)', Context)
  147. test3 = LispG2.DoParse1( '(x)', Context)
  148. test4 = LispG2.DoParse1( '" a string "', Context)
  149. test5 = LispG2.DoParse1( '(setq y (1 2 3) )', Context )
  150. test6 = LispG2.DoParse1( '(SeTq x ("a string" "another" 0))', Context )
  151. test7str = """
  152.   ; this is a lisp comment
  153.   (setq abc (("a" x)
  154.              ("b" (setq d 12))
  155.              ("c" y) ) ; another lisp comment
  156.   )
  157. """
  158. test7 = LispG2.DoParse1( test7str, Context)
  159. test8 = LispG2.DoParse1( '(print (1 x d))', Context)
  160.  
  161.